#!/bin/bash

path=$(cd "$(dirname "$0")"; pwd)
real_path=""
i=0
for pt in $path ; do
	if [ $i == 0 ] ; then
		real_path=$pt
		i=1
	else
		real_path=$real_path"\ "$pt
	fi
done
path=$real_path
cd $path

pic_path="background/default.jpg"
if [ $# -gt 0 ]; then
	# pic_path or randweb or randlocal or save
	pic_path=$1
	extension="${pic_path##*.}"
	if [ ${pic_path:0:3} == "--h" ]; then
		echo -e "useage -> wallpaper [para1] [para2] [para3] \n\t\teach para can be empty[means no function]\n\t\twhat's more, \"xxx\" means you should input this string to apply the function \n"
		echo -e "para1 : your_wallpaper_path    :  wallpaper from yourself local path, like background/default.jpg \n" \
			    "       \"randweb\"        :  wallpaper from web random , this will take a few seconds, please be patience\n" \
			    "       \"randlocal\"      :  wallpaper from background random"
		echo "para2 : \"com\"            :  composite code calender "
		echo "para3 : number[0-52]     :  this means pass week counter of code calender, with para2"
		exit -1
	elif [ $extension != "jpg" -a $extension != "png" -a $extension != "randweb" -a $extension != "randlocal" -a "save" ]; then
		echo "first para should be your_wallpaper_path or randweb or randlocal or save"
		exit -1
	fi
fi

composite="no"
pass_week=0
week_num=`date +%V`
if [ $# -gt 1 ]; then
	composite=$2
	if [ ${composite:0:3} == "com" ] ; then
		composite="yes"
	else
		composite="no"
	fi
	if [ $# -gt 2 ]; then
		pass_week=$3
		if [ $pass_week -ge $week_num ] ; then
			week_num="01"
		else
			#week_num=`date +%V -d -${pass_week}weeks`
			week_num=$(($week_num -${pass_week}))
			if [ ${#week_num} -eq 1 ] ; then
				week_num="0"$week_num
			fi
		fi
	fi
	if [ ! -d composite ] ; then
		mkdir composite 
	fi
fi

function random()
{
    max=$1
    num=$(date +%s)
    ((retnum=num%max+1))
    echo $retnum
}

localpath=$path"/"$pic_path

function get_pic() {
	if [ $pic_path == "randweb" ]; then
		web_list=`head -1 source/web_info | cut -d ' ' -f 1`
		web_page=`head -1 source/web_info | cut -d ' ' -f 2`
		agent=`tail -n1 source/web_info`
		curl -o source/wallhaven -A "$agent" "$web_list" > /dev/null 2>&1 
		info=`cat source/wallhaven`
		head_info=${info:13000:15000}
		pic_id=`expr "$head_info" : ".*data-wallpaper-id=\"\(.*\)\" style=.*"`
		pic_url="$web_page$pic_id.jpg"
		time=`date +%s`
		random_pic_path="background/random"$time".jpg"
		rm -f background/random*
		curl -o $random_pic_path "$pic_url" > /dev/null 2>&1 
		if [ `grep 404 $random_pic_path | wc -l` -gt 0 ]; then
			pic_url="$web_page$pic_id.png"
			random_pic_path="background/random"$time".png"
			curl -o $random_pic_path "$pic_url" > /dev/null 2>&1
		fi
		pic_path=$random_pic_path
	elif [ $pic_path == "randlocal" ]; then
		max_c=`ls background | wc -l`
		random_val=$(random $max_c)
		pic_path="background/"`ls background | sed -n "${random_val}p" `
	elif [ $pic_path == "save" ]; then
        save_file=`ls -l -t background/ | head -2 | tail -1 | awk '{print $NF}'`
		if [ ${save_file:0:6} == "random" ] ; then
			mv -f background/$save_file background/x_$save_file
		fi
        exit 0
	fi
	localpath=$path"/"$pic_path
}

function composite() {
	if [ $composite == "yes" ]; then
		python bin/generate_code_calender.py $pic_path $week_num
		localpath=$path"/composite/code_calendar_wallpaper_$week_num.jpg"
	fi
}

function set_pic() {
	#time=$(date +%F_%X)
	#cp -f $localpath $localpath$time
	#osascript -e "tell application \"Finder\" to set desktop picture to POSIX file \"$localpath$time\""
	#rm -f $localpath$time
	osascript -e "tell application \"Finder\" to set desktop picture to POSIX file \"$localpath\""
}

get_pic
composite
set_pic
